/* OSM layers for gvSIG. 
 * Geographic Information System of the Valencian Government
*
* Copyright (C) 2012 Nacho Brodin
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
* MA  02110-1301, USA.
* 
*/

package org.gvsig.raster.osm.io;

import org.gvsig.raster.impl.store.AbstractRasterDataParameters;
import org.gvsig.tools.ToolsLocator;
import org.gvsig.tools.dataTypes.DataTypes;
import org.gvsig.tools.dynobject.DelegatedDynObject;
import org.gvsig.tools.dynobject.DynClass;
import org.gvsig.tools.dynobject.DynField;
import org.gvsig.tools.dynobject.DynObjectManager;
import org.gvsig.tools.dynobject.DynStruct;
import org.gvsig.tools.persistence.PersistenceManager;
import org.gvsig.tools.persistence.PersistentState;
import org.gvsig.tools.persistence.exception.PersistenceException;

/**
 * Parameters for OSM provider
 * @author Nacho Brodin (nachobrodin@gmail.com)
 */
public class OSMDataParametersImpl extends AbstractRasterDataParameters implements OSMDataParameters {
	public static final String            PARAMETERS_DEFINITION_NAME = "OSMRasterDataParameters";
	private static DynClass               DYNCLASS                   = null;
	private DelegatedDynObject            delegatedDynObject         = null;
	private static final String           FIELD_WIDTH                = "width";
	private static final String           FIELD_HEIGHT               = "height";
	private static final String           FIELD_MAX_RES_LEVEL        = "maxResolutionLevel";
	private static final String           FIELD_NAME                 = "name";
	private static final String           FIELD_SUFFIX               = "suffix";
	
	public OSMDataParametersImpl() {
		super();
		initialize();
	}
	
	protected void initialize() {
		delegatedDynObject = (DelegatedDynObject) ToolsLocator
				.getDynObjectManager().createDynObject(
						DYNCLASS);
	}
	
	@SuppressWarnings("deprecation")
	public static void registerDynClass() {
		DynObjectManager dynman = ToolsLocator.getDynObjectManager();
		DynClass dynClass;
		DynField field;
		
		if(dynman == null)
			return;
		
		if (DYNCLASS == null) {
			dynClass = AbstractRasterDataParameters.registerDynClass(PARAMETERS_DEFINITION_NAME);
			//dynClass = dynman.add(AbstractRasterDataParameters.DYNCLASS_NAME);
			
			field = dynClass.addDynField(FIELD_WIDTH);
			field.setTheTypeOfAvailableValues(DynField.ANY);
			field.setDescription("Width");
			field.setType(DataTypes.INT);
			field.setMandatory(false);
			
			field = dynClass.addDynField(FIELD_HEIGHT);
			field.setTheTypeOfAvailableValues(DynField.ANY);
			field.setDescription("Height");
			field.setType(DataTypes.INT);
			field.setMandatory(false);
			
			field = dynClass.addDynField(FIELD_MAX_RES_LEVEL);
			field.setTheTypeOfAvailableValues(DynField.ANY);
			field.setDescription("Maximun levels of resolution");
			field.setType(DataTypes.INT);
			field.setMandatory(false);
			
			field = dynClass.addDynFieldString(FIELD_NAME);
			field.setDescription("Layer name");
			field.setClassOfValue(String.class);
			field.setMandatory(false);
			
			field = dynClass.addDynFieldString(FIELD_SUFFIX);
			field.setDescription("Tile suffix");
			field.setClassOfValue(String.class);
			field.setMandatory(false);

			DYNCLASS = dynClass;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.gvsig.fmap.dal.DataStoreParameters#getDataStoreName()
	 */
	public String getDataStoreName() {
		return OSMProvider.NAME;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.gvsig.fmap.dal.DataStoreParameters#getDescription()
	 */
	public String getDescription() {
		return OSMProvider.DESCRIPTION;
	}
	
	public static void registerPersistence() {
		PersistenceManager manager = ToolsLocator.getPersistenceManager();
		DynStruct definition = manager.getDefinition("OSMDataParameters_Persistent");
		if( definition == null ) {
			definition = manager.addDefinition(
					OSMDataParametersImpl.class,
					"OSMDataParameters_Persistent",
					"OSM DataParameters Persistency",
					null, 
					null
			);
			AbstractRasterDataParameters.registerPersistence(definition);
			
			/*definition.addDynFieldString(FIELD_VARIABLE).setMandatory(false);
			definition.addDynFieldInt(FIELD_LEVEL).setMandatory(false);
			definition.addDynFieldInt(FIELD_TIME).setMandatory(false);
			definition.addDynFieldString(FIELD_XDIM).setMandatory(false);
			definition.addDynFieldString(FIELD_YDIM).setMandatory(false);*/
		}
	}
	
	@Override
	public void saveToState(PersistentState state) throws PersistenceException {
		super.saveToState(state);
		
		/*state.set(FIELD_VARIABLE, getStringVariable());	
		state.set(FIELD_LEVEL, getFieldLevel());
		state.set(FIELD_TIME, getFieldTime());
		state.set(FIELD_XDIM, getXDimVariable());
		state.set(FIELD_YDIM, getYDimVariable());*/
	}
	
	@Override
	public void loadFromState(PersistentState state)
			throws PersistenceException {
		super.loadFromState(state);
		
		/*setFieldVariable(state.getString(FIELD_VARIABLE));
		setFieldTime(state.getInt(FIELD_TIME));
		setFieldLevel(state.getInt(FIELD_LEVEL));
		xDimVariable = state.getString(FIELD_XDIM);
		yDimVariable = state.getString(FIELD_YDIM);*/
	}

	@Override
	protected DelegatedDynObject getDelegatedDynObject() {
		return delegatedDynObject;
	}

	public boolean isOverridingHost() {
		return false;
	}

	public void setOverrideHost(boolean over) {
		
	}
	
	public void setWidth(int w) {
		this.setDynValue(FIELD_WIDTH, new Integer(w));
	}
	
	public void setHeight(int h) {
		this.setDynValue(FIELD_HEIGHT, new Integer(h));
	}
	
	public int getWidth() {
		Integer b = (Integer)getDynValue(FIELD_WIDTH);
		if(b != null)
			return ((Integer)b).intValue();
		return 0;
	}
	
	public int getHeight() {
		Integer b = (Integer)getDynValue(FIELD_HEIGHT);
		if(b != null)
			return ((Integer)b).intValue();
		return 0;
	}
	
	public String getOSMLayerName() {
		String b = (String)getDynValue(FIELD_NAME);
		if(b != null)
			return b;
		return null;
	}
	
	public void setOSMLayerName(String name) {
		this.setDynValue(FIELD_NAME, name);
	}
	
	public int getNumberOfLevels() {
		Integer b = (Integer)getDynValue(FIELD_MAX_RES_LEVEL);
		if(b != null)
			return ((Integer)b).intValue();
		return 0;
	}
	
	public void setNumberOfLevels(int levels) {
		this.setDynValue(FIELD_MAX_RES_LEVEL, new Integer(levels));
	}
	
	public String getTileSuffix() {
		String b = (String)getDynValue(FIELD_SUFFIX);
		if(b != null)
			return b;
		return null;
	}
	
	public void setTileSuffix(String suffix) {
		this.setDynValue(FIELD_SUFFIX, suffix);
	}
}
